Geolocation based Speedometer and Compass


Posted by wayne201299 on 2023-10-03

DEMO

取得目前移動速度,以及指北針

實作

  1. 同上篇,要取得瀏覽器定位功能所以要先啟動本地server
  2. 監聽Web API 的地理位置變化事件

     const arrow = document.querySelector(".arrow");
     const speed = document.querySelector(".speed-value");
    
     navigator.geolocation.watchPosition(
         (position) => {
             speed.textContent = position.coords.speed;
             arrow.style.transform = `rotate(${position.coords.heading}deg)`;
         },
         (err) => {
             console.log(err);
             alert("Geolocation is not supported by this browser.");
         }
     );
    

總結

  • 了解如何透過navigator.geolocation取得目前地理位址

#javascript







Related Posts

Python 串列 list 和元組 tuple入門教學

Python 串列 list 和元組 tuple入門教學

Day01 監聽“可見狀態”事件 - Intersection Observer API

Day01 監聽“可見狀態”事件 - Intersection Observer API

[ 學習筆記系列 ] 網頁本質 (一) - HTML 篇

[ 學習筆記系列 ] 網頁本質 (一) - HTML 篇


Comments